package controller

import (
	"github.com/gin-gonic/gin"
	"k8s-dash/dto"
	"k8s-dash/middleware"
	"k8s-dash/service"
	"k8s-dash/utils"
	"k8s-dash/utils/logger"
)

type dsController struct{}

var DsController dsController

func (p *dsController) Register(ginRouter *gin.RouterGroup) {
	ds := &dsController{}
	ginRouter.GET("/k8s/daemonset/list", ds.GetDsList)
	ginRouter.PATCH("/k8s/daemonset/updaet", ds.UpdateDs)
	ginRouter.GET("/k8s/daemonset/detail", ds.GetDsDetail)
	ginRouter.DELETE("/k8s/daemonset/del", ds.DeleteDs)
}

// GetDsList 获取daemonset列表，支持分页过滤排序
// ListPage godoc
// @Summary      获取daemonset列表
// @Description  获取daemonset列表
// @Tags         daemonset
// @ID           /api/daemonset/list
// @Accept       json
// @Produce      json
// @Param        filter_name  query  string  false  "过滤"
// @Param        namespace    query  string  false  "命名空间"
// @Param        page         query  int     false  "页码"
// @Param        limit        query  int     false  "分页限制"
// @Success      200 {object}  middleware.Response"{"code": 200, msg="","data": service.DsResp}"
// @Router       /api/k8s/daemonset/list [get]
func (d *dsController) GetDsList(ctx *gin.Context) {
	parmas := &dto.DsListInput{}
	//参数验证
	err := parmas.BindValidate(ctx)
	if err != nil {
		logger.LG.Error(utils.GetErrorMsg(utils.ParamBindError))
		middleware.ResponseError(ctx, utils.NewGlobalError(utils.ParamBindError, err))
		return
	}
	dsList, err := service.Daemonset.GetDsList(parmas.FilterName, parmas.Namespace, parmas.Limit, parmas.Page)
	if err != nil {
		logger.LG.Error(utils.GetErrorMsg(utils.GetError))
		middleware.ResponseError(ctx, utils.NewGlobalError(utils.GetError, err))
		return
	}
	middleware.ResponseSuccess(ctx, dsList)
}

// GetDsDetail 获取daemonset详情
// ListPage godoc
// @Summary      获取daemonset详情
// @Description  获取daemonset详情
// @Tags         daemonset
// @ID           /api/daemonset/detail
// @Accept       json
// @Produce      json
// @Param        name  query  string  true  "name"
// @Param        namespace    query  string  true  "命名空间"
// @Success      200 {object}  middleware.Response"{"code": 200, msg="","data": appsV1.Daemonset}"
// @Router       /api/k8s/daemonset/detail [get]
func (d *dsController) GetDsDetail(ctx *gin.Context) {
	parmas := &dto.DsNameInput{}
	if err := parmas.BindValidate(ctx); err != nil {
		logger.LG.Error(utils.GetErrorMsg(utils.ParamBindError))
		middleware.ResponseError(ctx, utils.NewGlobalError(utils.ParamBindError, err))
	}
	dsDetail, err := service.Daemonset.GetDsDetail(parmas.Name, parmas.Namespace)
	if err != nil {
		return
	}
	if err != nil {
		logger.LG.Error(utils.GetErrorMsg(utils.GetError))
		middleware.ResponseError(ctx, utils.NewGlobalError(utils.GetError, err))
		return
	}
	middleware.ResponseSuccess(ctx, dsDetail)
}

// DeleteDs 删除ds
// ListPage godoc
// @Summary      删除ds
// @Description  删除ds
// @Tags         daemonset
// @ID           /api/daemonset/delete
// @Accept       json
// @Produce      json
// @Param        name  query  string  true  "name"
// @Param        namespace    query  string  true  "命名空间"
// @Success      200 {object}  middleware.Response"{"code": 200, msg="","data": nil}"
// @Router       /api/k8s/daemonset/delete [delete]
func (d *dsController) DeleteDs(ctx *gin.Context) {
	parmas := &dto.DsNameInput{}
	if err := parmas.BindValidate(ctx); err != nil {
		logger.LG.Error(utils.GetErrorMsg(utils.ParamBindError))
		middleware.ResponseError(ctx, utils.NewGlobalError(utils.ParamBindError, err))
	}
	err := service.Daemonset.DeleteDs(parmas.Name, parmas.Namespace)
	if err != nil {
		logger.LG.Error(utils.GetErrorMsg(utils.DeleteError))
		middleware.ResponseError(ctx, utils.NewGlobalError(utils.DeleteError, err))
		return
	}
	middleware.ResponseSuccess(ctx, nil)
}

// UpdateDs 更新ds
// ListPage godoc
// @Summary      更新ds
// @Description  更新ds
// @Tags         daemonset
// @ID           /api/daemonset/update
// @Accept       json
// @Produce      json
// @Param        content  query  string  true  "更新内容"
// @Param        namespace    query  string  true  "命名空间"
// @Success      200 {object}  middleware.Response"{"code": 200, msg="","data": nil}"
// @Router       /api/k8s/daemonset/update [patch]
func (d *dsController) UpdateDs(ctx *gin.Context) {
	parmas := &dto.DsUpdateInput{}
	if err := parmas.BindValidate(ctx); err != nil {
		logger.LG.Error(utils.GetErrorMsg(utils.ParamBindError))
		middleware.ResponseError(ctx, utils.NewGlobalError(utils.ParamBindError, err))
	}
	err := service.Daemonset.UpdateDs(parmas.Content, parmas.Namespace)
	if err != nil {
		logger.LG.Error(utils.GetErrorMsg(utils.UpdateError))
		middleware.ResponseError(ctx, utils.NewGlobalError(utils.UpdateError, err))
		return
	}
	middleware.ResponseSuccess(ctx, nil)
}
